<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>退款订单管理</title>
    <link rel="stylesheet" href="/static/lib/layui-v2.5.6/layui-v2.5.6/layui/css/layui.css">
    <style>
        .layui-table-cell{
            height:36px;
            line-height: 36px;
        }
        .layui-col-xs1{
            height: 66px;
            line-height: 66px;
            margin: 5px 5px 0 0;
            text-align: center;
            font-size: 20px;
            cursor: pointer;
        }
        .info{
            font-size: 14px;
            color: rgb(110, 108, 108);
            line-height: 36px;
        }
        .layui-icon{
            cursor: pointer;
        }
        
    </style>
</head>
<body>
    <table id="demo" lay-filter="test"></table>


<!-- 订单详细页面 -->
    <div id="orderDetails" style="display: none;">
        <div class="layui-form-item">
            <label class="layui-form-label">订单号</label>
            <div class="layui-input-block id_info1 info"></div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block userid_info1 info"></div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">买家编号</label>
            <div class="layui-input-block buyerid_info1 info"></div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">订单时间</label>
            <div class="layui-input-block createdate_info1 info"></div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">订单状态</label>
            <div class="layui-input-block orderstatus_info1 info"></div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">付款方式</label>
            <div class="layui-input-block paymethod_info1 info"></div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">联系方式</label>
            <div class="layui-input-block phone_info1 info"></div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">付款订单</label>
              <div class="layui-input-inline payorderid_info1 info"></div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">付款时间</label>
              <div class="layui-input-inline paydate_info1 info"></div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">机器ID</label>
              <div class="layui-input-inline machineid_info1 info"></div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">格子柜号</label>
              <div class="layui-input-inline containernumber_info1 info"></div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">订单总价</label>
            <div class="layui-input-block amount_info1 info"></div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block comment_info1 info"></div>
        </div>

        <table class="layui-table" lay-skin="line">
            <thead>
                <tr>
                  <th>商品名称</th>
                  <th>商品规格</th>
                  <th>商品单价</th>
                  <th>商品数量</th>
                </tr> 
            </thead>
            <tbody id="commodities_info1"></tbody>
        </table>

    </div>




    <script type="text/html" id="barDemo">
        <i class="layui-icon layui-icon-tabs" lay-event="orderDetails_1" title="订单详情" ></i>
        <i class="layui-icon layui-icon-ok" lay-event="approveRefund_1" title="同意退款申请"></i>
        <i class="layui-icon layui-icon-close" lay-event="refuseRefund_1" title="拒绝退款申请"></i>
    </script>

    
    <script src="/static/lib/layui-v2.5.6/layui-v2.5.6/layui/layui.js"></script>
    <script>
        
        layui.use(['table','util'], function(){
            var $ = layui.jquery;
            var table = layui.table;
            var util = layui.util;

            table.render({
                elem: '#demo',
                height: 'full-60',
                                //TODO:
                url: '/admin/bookOrderAdmin/getBookOrderRefundApply', //数据接口
                page: {layout: ['prev', 'page', 'next', 'skip', 'count']}, //开启分页
                toolbar: '#toolbarDemo',
                defaultToolbar: [],
                id: 'adTest',
                cols: [[ //表头
                {field: 'id', title: 'ID', width:'12%', sort: true, fixed: 'left'},
                {field: 'bool_order_id', title: '退款订单号', width:'20%',},
                {field: 'refund_time', title: '退款时间', templet:"<div>{{layui.util.toDateString(Number(d.refund_time), 'yyyy.MM.dd HH:mm:ss')}}</div>", width:'15%', sort: true},
                {field: 'refund_reason', title: '退款原因', width: '35%'},
                {field: 'status', title: '退款状态', width: '10%', templet:'<div>{{refundApplystatusfun(d.status)}}</div>', sort: true},
                {fixed: 'right', width:'8%', align:'center', toolbar: '#barDemo'}
                ]],


                parseData: function(res){ //res 即为原始返回的数据
                    return {
                    "code": res.success, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.list  //解析数据列表
                    };
                },
                request: {
                    pageName: 'pageNum', //页码的参数名称，默认：page
                    limitName: 'pageSize' //每页数据量的参数名，默认：limit
                },
                response: {
                    // statusName: 'success', //规定数据状态的字段名称，默认：code
                    statusCode: true //规定成功的状态码，默认：0
                }
            });
        
            

            table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
                if(layEvent === 'orderDetails_1'){
                    $.ajax({
                        type: "post",
                                //TODO:
                        url: "/admin/bookOrderAdmin/getOrderDetail",
                        data: {order_id: data.bool_order_id},
                        dataType: "json",
                        // async: false,
                        success: function(mag){
                            $(".id_info1").html('<span style="color: #f173e1;">'+mag.data.id+'</span>')
                            $(".userid_info1").html(mag.data.user.name)
                            $(".buyerid_info1").html(status(mag.data.buyer_id))
                            $(".createdate_info1").html(ctime(mag.data.create_date))
                            $(".orderstatus_info1").html(orderstatusfun(mag.data.order_status))
                            $(".machineid_info1").html(mag.data.machine_id)
                            $(".containernumber_info1").html(status(mag.data.container_number))
                            $(".amount_info1").html('<span style="color: #f173e1;">'+mag.data.amount+'</span>')
                            $(".comment_info1").html(mag.data.comment)
                            $(".paymethod_info1").html(paymethodfun(mag.data.pay_method))
                            $(".payorderid_info1").html(status(mag.data.pay_order_id))
                            $(".paydate_info1").html(ctime(mag.data.pay_date))
                            $(".phone_info1").html(mag.data.user.phone)
                            var commodities = mag.data.commodities
                            if (commodities.length != 0) {
                                var flag = ''
                                for (var i = 0; i < commodities.length; i++) {
                                    var flag1 = '<tr><td>'+ commodities[i].commodityName +'</td><td>'+ commodities[i].specifications +'</td><td>'+ commodities[i].price +'</td><td>'+ commodities[i].count +'</td></tr>' 
                                }
                                flag += flag1
                                $("#commodities_info1").html(flag)
                            }
                              
                        }
                    })
                    layer.open({
                        type: 1,
                        skin: 'layui-layer-rim', //加上边框
                        area: ['66%', '90%'], //宽高
                        title: '预约订单详细',
                        btn: ['同意','拒绝'],
                        content: $('#orderDetails'),
                        yes:function(index){
                            layer.confirm('同意退款？', function(index2){
                                $.ajax({
                                    type: "post",
                                            //TODO:
                                    url: "/admin/bookOrderAdmin/approveRefund",
                                    data: {bookOrderId: data.bool_order_id,refundApplyId: data.id,refund_reason: data.refund_reason},
                                    dataType: "json",
                                    success: function(mag){
                                        if (mag.success == true) {
                                            layer.msg(mag.message)
                                            obj.update({
                                                status: '1'
                                            });
                                            layer.close(index);
                                        }else{
                                            layer.msg(mag.message)
                                        }
                                    },
                                    error:function(){
                                        layer.msg('操作失败,请重新登录操作')
                                    }
                                })
                            })
                            // layer.close(index);
                        },
                        btn2:function(index){
                            layer.confirm('拒绝退款后无法更改？', function(index2){
                                $.ajax({
                                    type: "post",
                                            //TODO:
                                    url: "/admin/bookOrderAdmin/refuseRefund",
                                    data: {bookOrderId: data.bool_order_id,refundApplyId: data.id},
                                    dataType: "json",
                                    success: function(mag){
                                        if (mag.success == true) {
                                            layer.msg(mag.message)
                                            obj.update({
                                                status: '2'
                                            });
                                            layer.close(index);
                                        }else{
                                            layer.msg(mag.message)
                                        }
                                    },
                                    error:function(){
                                        layer.msg('操作失败,请重新登录操作')
                                    }
                                })
                            })
                        return false
                        },
                    });
                } else if(layEvent === 'approveRefund_1'){
                    layer.confirm('同意退款？', function(index){
                        $.ajax({
                            type: "post",
                                    //TODO:
                            url: "/admin/bookOrderAdmin/approveRefund",
                            data: {bookOrderId: data.bool_order_id,refundApplyId: data.id,refund_reason: data.refund_reason},
                            dataType: "json",
                            success: function(mag){
                                if (mag.success == true) {
                                    layer.msg(mag.message)
                                    obj.update({
                                        status: '1'
                                    });
                                }else{
                                    layer.msg(mag.message)
                                }
                            },
                            error:function(){
                                layer.msg('操作失败,请重新登录操作')
                            }
                        })
                    })
                } else if(layEvent === 'refuseRefund_1'){
                    layer.confirm('拒绝退款后无法更改？', function(index){
                        $.ajax({
                            type: "post",
                                    //TODO:
                            url: "/admin/bookOrderAdmin/refuseRefund",
                            data: {bookOrderId: data.bool_order_id,refundApplyId: data.id},
                            dataType: "json",
                            success: function(mag){
                                if (mag.success == true) {
                                    layer.msg(mag.message)
                                    obj.update({
                                        status: '2'
                                    });
                                }else{
                                    layer.msg(mag.message)
                                }
                            },
                            error:function(){
                                layer.msg('操作失败,请重新登录操作')
                            }
                        })
                    })


                }
            });
        
        
        });
        function status(num) {
            if(num == null) {
                var status = '订单未完成'
            }else {
                var status = num
            }
            return status
        }
        
        function dateTime(date) {
            if (date != '') {
                var dateTime = new Date(date).getTime()
            }else{
                var dateTime = ''
            }
            return dateTime
        }
        
        function ctime(num) {
            if (num == null) {
                var ctime = '订单未完成'
            }else {
                var ctime = layui.util.toDateString(Number(num), "yyyy.MM.dd HH:mm:ss")
            }
            return ctime
        }
        
        function refundApplystatusfun(num) {
            if (num == 0) {
                var refundApplystatus = '<span style="color: rgb(222, 130, 235);">申请退款</span>'
            }else if (num == 1) {
                var refundApplystatus = '同意退款'
            }else if (num == 2) {
                var refundApplystatus = '拒绝退款'
            }
            return refundApplystatus
        }

        function orderstatusfun(num) {
            if (num == 0) {
                var orderstatus = '未付款'
            }else if (num == 1) {
                var orderstatus = '<span style="color: rgb(222, 130, 235);">未发货</span>'
            }else if (num == 2) {
                var orderstatus = '未取货'
            }else if (num == 3) {
                var orderstatus = '订单完成'
            }else if (num == 6) {
                var orderstatus = '取消订单'
            }else if (num == 8) {
                var orderstatus = '退款订单'
            }else{
                var orderstatus = '订单未完成'
            }
            return orderstatus
        }
        
        function paymethodfun(num) {
            if (num == '0') {
                var paymethod = '支付宝'
            }else if (num == '1'){
                var paymethod = '微信'
            }else if (num == '2'){
                var paymethod = '账户余额'
            }else {
                var paymethod = '其他支付方式'
            }
            return paymethod
        }
    </script>
</body>
</html>